package com.ce.webframework.sec.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.sitechasia.webx.core.dao.hibernate3.HibernateDomainObjectDao;
import com.ce.webframework.sec.dao.ISecToolbarDao;
import com.ce.webframework.sec.model.SecToolbarDO;
import com.ce.webframework.sec.model.SecToolbarTempDO;

public class SecToolbarDaoImpl extends HibernateDomainObjectDao<SecToolbarDO> implements ISecToolbarDao{

	/**
	 *
	 * 功能描述:查询当前用户所有已应用的快捷菜单(根据应orderNum字段排序)
	 * @param userId 当前用户ID
	 * @param epId 开发商ID
	 * @param 权限功能集合
	 *@return List
	 */
	@SuppressWarnings("unchecked")

	public List<SecToolbarDO> getUseCutMenus(Long userId,Long epId,List <String>functionIds) {
		if(functionIds==null) return new ArrayList<SecToolbarDO>();
		String funcIdsSplit = "";

		for(int i=0;i<functionIds.size();i++){
			System.out.println("functionIds=========="+i+" : "+functionIds.get(i).toString());
			String funcId = "'"+functionIds.get(i).toString()+"'";
			funcIdsSplit+=funcId;
			if(i!=functionIds.size()-1)
				funcIdsSplit+=",";
		}

		String hsql = "FROM SecToolbarDO menu LEFT JOIN FETCH menu.secToolbarTemp temp " +
		  "WHERE temp.functionEntityId in ("+funcIdsSplit+") AND menu.userId='"+userId+"' AND menu.epId='"+epId+"' ORDER BY menu.orderNum ASC";
		System.out.println("hsql===============>"+hsql);
		List<SecToolbarDO> menuList = getHibernateTemplate().find(hsql);
		System.out.println("menuList=======>"+menuList.size());
		return menuList;
	}



	/**
	 * 快捷菜从应用到未应用更新序号
	 *@param toolbarDO 应用快捷菜单对象
	 *@param epId 开发商ID
	 *@param userId 用户ID
	 *
	 */

	public void updateCutMenu(SecToolbarDO toolbarDO,  Long epId, Long userId) {
		Session session = getSession();
		String updateOldDtsSql = "UPDATE SecToolbarDO tool" +
		 " SET tool.orderNum = tool.orderNum - 1" +
		 " WHERE tool.orderNum > " + toolbarDO.getOrderNum().intValue();

		Query query2 = session.createQuery(updateOldDtsSql);
		query2.executeUpdate();
		updateOldDtsSql = null;

	}



	/**
	 * 快捷菜从未应用到应用更新序号
	 *@param orderNum 应用快捷菜单序号
	 *@param epId 开发商ID
	 *@param userId 用户ID
	 */
	public void updateCutMenu(String orderNum, Long epId, Long userId) {
		Session session = getSession();
//		更新目的容器拖拉源序号
		String updateNewDtsSql = "UPDATE SecToolbarDO cut" +
								 " SET cut.orderNum = cut.orderNum + 1" +
								 " WHERE cut.orderNum >=" + orderNum;


		Query query = session.createQuery(updateNewDtsSql);
		query.executeUpdate();
		updateNewDtsSql = null;
	}



	/**
	 * 应用快捷菜单内部上下拖拉
	 *@param toolbarDO 应用快捷菜单对象
	 *@param orderNum 应用快捷菜单序号
	 *@param epId 开发商ID
	 *@param userId 用户ID
	 */
	public void updateCutMenu(SecToolbarDO toolbarDO, String orderNum, Long epId, Long userId) {
		Session session = getSession();
		String updateSiteSql = "";
		if (new Integer(orderNum).intValue() > toolbarDO.getOrderNum().intValue()){

			updateSiteSql =
							" UPDATE SecToolbarDO cut SET cut.orderNum = cut.orderNum - 1" +
							" WHERE cut.orderNum <=" + orderNum +
							" AND cut.orderNum >" + toolbarDO.getOrderNum().intValue();
		}else{
			updateSiteSql = "UPDATE SecToolbarDO cut" +
							" SET cut.orderNum = cut.orderNum + 1" +
							" WHERE cut.orderNum >=" + orderNum +
							"AND cut.orderNum <" + toolbarDO.getOrderNum().intValue();
		}


		String updateSelf = "UPDATE SecToolbarDO tool SET tool.orderNum='"+orderNum+"' WHERE tool.toolbarId='"+toolbarDO.getToolbarId()+"'" ;

		session.createQuery(updateSiteSql).executeUpdate();

		session.createQuery(updateSelf).executeUpdate();
		updateSiteSql = null;
	}

	/**
	 *
	 * 功能描述: 查询当前用户是否有已应用的快捷菜单
	 *
	 *@param toolBarTempId 菜单模板Id
	 *@param userId 用户ID
	 *@return int
	 */

	public int findToolbarByTempId(Long toolBarTempId,Long userId){
		String hsql = "SELECT COUNT(*) FROM SecToolbarDO tool where tool.secToolbarTemp.toolbarTempId='"+toolBarTempId+"' " +
				"AND tool.userId='"+userId+"'";
		int recordSize = new Integer(getHibernateTemplate().find(hsql).get(0).toString());
		System.out.println("recordSize===============:"+recordSize);
		return recordSize;

	}

}
